import java.net.*;
import java.io.*;

public class code {
	
	 

	  public static void main(String[] args)
	  {
		  int port = 8081;
		  String FILE_TO_SEND = "c:/Users/Patrice/Downloads/Snap3.jpg";
		  ServerSocket socketServeur = null;
		  Socket socketClient = null;  		
		  
				    try 
				    {
					      socketServeur = new ServerSocket(port);
					      System.out.println("Lancement du serveur");
				
					      while (true) 
					      {
					    	  socketClient = socketServeur.accept();
					    	  System.out.println("Connexion avec : "+socketClient.getInetAddress());
					         
					          // reception
					          BufferedReader in =  new BufferedReader(  new InputStreamReader(socketClient.getInputStream()));
					          String commande = in.readLine ();
					          System.out.println ("Client reception: " + commande);
					      		
					      
					          if (commande.equals("SEND"))
								{
					        	   /* FileInputStream fis = null;
					        	    BufferedInputStream bis = null;
					        	    OutputStream os = null;
								
					        	    try 
					        	    {
					        	    System.out.println("Serveur envoi un fichier au client");
									// send file
							          File myFile = new File (FILE_TO_SEND);
							          byte [] mybytearray  = new byte [(int)myFile.length()];
							          fis = new FileInputStream(myFile);
							          bis = new BufferedInputStream(fis);
							          bis.read(mybytearray,0,mybytearray.length);
							          os = socketClient.getOutputStream();
							          System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
							          os.write(mybytearray,0,mybytearray.length);
							          os.flush();
							          System.out.println("Done.");
					        	    }
							          
							          
							          finally {
							              if (bis != null) bis.close();
							              if (os != null) os.close();
							              if (socketClient!=null) socketClient.close();
							            }
									*/
					        	  	  System.out.println("Serveur envoi un fichier au client");
					        	  	 File myFile = new File("d:/toto.txt");
					        	   
					        	      byte[] mybytearray = new byte[(int) myFile.length()];
					        	      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
					        	      bis.read(mybytearray, 0, mybytearray.length);
					        	      OutputStream os = socketClient.getOutputStream();
					        	     // os.write(mybytearray, 0, mybytearray.length);
					        	     // os.flush();
					        	      System.out.println("Done.");
					        	  
					        	  
								}
					          else
					          {
					        	  System.out.println("Autre chose");
					        	  
					          }

					          //socketServeur.close();
					         // socketClient.close();
					          
					      }
				    
				   } 
				   
				   catch (Exception e) 
				   {
				      e.printStackTrace();
				   }
				   
				    finally {
				        if (socketServeur != null)
							try {
								socketServeur.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
				      }
				    
				   
	  }
}